home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15464 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Is this a memory leak?
  5. Date: Fri, 05 Apr 1996 12:04:01 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <31655281.78CB@datalytics.com>
  8. References: <4jv214$gv7@insosf1.netins.net> <4k0c2i$h6e@werple.net.au>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. David White wrote:
  16. > hhowe@trgnet.com (Harold Howe) writes:
  17. > >Could someone please tell me if this code leaks memory
  18. > >class BuriedClass
  19. > >  {
  20. > >  ...
  21. > >  }
  22. > >class TopClass
  23. > >  {
  24. > >  private
  25. > >    BuriedClass *bury;
  26. > >  public:
  27. > >    TopClass::TopClass() { bury = new BuriedClass();}
  28. > >    shutDown             { bury = 0;}
  29. > >    ~TopClass            { delete bury}
  30. > >  }
  31. > >int main(void)
  32. > >  {
  33. > >  TopClass *top = new TopClass();
  34. > >  top->shutDown();
  35. > >  delete top;
  36. > >  return 0;
  37. > >  }
  38. > [snip]
  39. > The BuriedClass object is not deleted. Zeroing the pointer does nothing
  40. > but zero the pointer, which prevents the BuriedClass object from being
  41. > deleted. Maybe 'shutDown' is intended to be called if responsibility for
  42. > the BuriedClass object is moved to something else, which will delete it,
  43. > or maybe it's a safety measure, to be used if some sort of free store
  44. > corruption is detected.
  45.  
  46. Possibly, shutDown was supposed to delete the memory before 
  47. setting the pointer to zero.
  48.  
  49. There is also another question.  Why is top allocated on the 
  50. heap?  I realize this is a short excerpt of code to illustrate 
  51. something else, but this is a common thing.  You should avoid 
  52. the heap whenever possible.  A heap allocation involves fairly 
  53. lengthy operations, while a stack allocation involves a 
  54. subtraction.  The result is higher performance.
  55.  
  56. -- 
  57. Robert Stewart        | My opinions are usually my own.
  58. Datalytics, Inc.    | stew@datalytics.com
  59.